<template>
    <!-- 会员管理 -->
    <div class="user">
        <!--搜索表单-->
        <!-- <div class="common-seach-wrap">
            <el-form size="small" :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="用户昵称">
                    <el-input v-model="searchForm.keyword" placeholder="关键字查询"></el-input>
                </el-form-item>
                <el-form-item label="下单途径">
                    <el-select>
                        <el-option label="小程序购买"></el-option>
                        <el-option label="后台赠送"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div> -->
        <div class="product-content">
            <div class="table-wrap">
                <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
                    <el-table-column prop="userCouponId" label="ID"></el-table-column>
                    <el-table-column prop="name" label="核销券标题"></el-table-column>
                    <el-table-column prop="userId" label="用户UID"></el-table-column>
                    <el-table-column prop="writeOffShopUserId" label="核销业务员UID"></el-table-column>
                    <el-table-column prop="nickName" label="用户昵称"></el-table-column>
                    <el-table-column prop="writeOffShopUserName" label="核销业务员姓名	"></el-table-column>
                    <el-table-column prop="writeOffTime" label="核销时间"></el-table-column>
                </el-table>
            </div>

            <!--分页-->
            <div class="pagination">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper" :total="totalDataNumber"></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import PackageApi from '@/api/package'
import { defaultPageSize } from '@/utils/table.js'
export default {
    data() {
        return {
            /*是否加载完成*/
            loading: false,
            /*列表数据*/
            tableData: [],
            /*一共多少条数据*/
            totalDataNumber: 0,
            searchForm: {
                pageIndex: 1,
                pageSize: defaultPageSize,
                keyword: '',
                pageSorts: []
            },
            leves: []
        };
    },
    created() {
        /*获取列表*/
        this.loadLevels()
        this.getTableList();

    },
    methods: {
        /*选择第几页*/
        handleCurrentChange(val) {
            let self = this;
            self.searchForm.pageIndex = val
            self.getTableList();
        },
        /*获取列表*/
        getTableList() {
            ///api/shop/plus/coupon/coupon/writeOffRecord
            let self = this;
            self.loading = true
            const Params = self.searchForm;
            PackageApi.writeOffRecord(Params, true)
                .then((res) => {
                    self.loading = false;
                    const { code, data } = res
                    if (code === 1) {
                        const { total } = data
                        self.totalDataNumber = total
                        self.tableData = data.records
                    }
                    console.log('券包购买记录数据', res)
                })
                .catch((error) => {
                    self.loading = false;
                });
        },
        /*搜索查询*/
        onSubmit() {
            let self = this;
            self.searchForm.pageIndex = 1;
            self.getTableList();
        },
        /*打开添加*/
        addClick(item) {
            PackageApi.exportRecord()
                .then(res => {
                    const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
                    const url = window.URL.createObjectURL(blob)
                    const a = document.createElement('a')
                    a.href = url
                    const fdate = new Date()
                    const perx = `${fdate.getFullYear()}-${fdate.getMonth() + 1}-${fdate.getDate()}`
                    a.download = perx + '券包购买记录.xlsx'
                    a.click()
                    setTimeout(() => {
                        a.remove()
                    }, 1000)
                })
        },
        /*打开编辑*/
        editClick(item) {
            let params = item.id;
            this.$router.push({
                path: "/user/coupon/edit",
                query: {
                    id: params,
                },
            });
        },
        viewClick(item) {
            let params = item.id;
            this.$router.push({
                path: "/user/coupon/view",
                query: {
                    id: params,
                },
            });
        },
        deleteClick(row) {
            let self = this;
            ElMessageBox.confirm("此操作将永久删除该记录, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    self.loading = true;
                    PackageApi.deleteCouponPack(
                        row.id,
                        true
                    )
                        .then((data) => {
                            self.loading = false;
                            if (data.code == 1) {
                                ElMessage({
                                    message: "恭喜你，删除成功",
                                    type: "success",
                                });
                                self.getTableList();
                            } else {
                                self.loading = false;
                            }
                        })
                        .catch((error) => {
                            self.loading = false;
                        });
                })
                .catch(() => { });
            //   deleteCouponPack
        },
        loadLevels() {
            const $this = this
            PackageApi.getAllGrade()
                .then(result => {
                    const { code, data } = result
                    if (code === 1) {
                        $this.leves = data
                    }
                })
        },
    },
};
</script>
<style></style>